﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace IloveRSS.Models.DataMap
{
    public class MembershipMapRepository
    {
        private ILoveRSSDataContext dataContext;

        public MembershipMapRepository(ILoveRSSDataContext dt)
        {
            this.dataContext = dt;
        }

        public MembershipUserCollection GetUsers()
        {
            return Membership.GetAllUsers();
        }

        public aspnet_Users GetUserByName(String userName)
        {
            return dataContext.aspnet_Users.FirstOrDefault(user => user.UserName.Equals(userName));
        }

        public bool DeleteUser(String userName)
        {
            var user = GetUserByName(userName);
            if (null != user)
            {
                user.Channels.Clear();
                dataContext.SubmitChanges();
                return Membership.DeleteUser(userName);
            }
            return false;
        }

        public Channels GetFavouriteChannel(string userName)
        {
            Users_Channels userChannel = dataContext.Users_Channels.FirstOrDefault(item => item.aspnet_Users.UserName == userName && item.IsFavourite);
            return (null != userChannel) ? userChannel.Channels : null;
        }

        public IEnumerable<Channels> GetChannelsByUserName(String userName)
        {
            return dataContext.Users_Channels.Where(item => item.aspnet_Users.UserName.Equals(userName)).Select(item => item.Channels);
        }

        public void SignIn(String userName, Channels channel)
         {
            if((null != channel) && (null != userName))
            {
                aspnet_Users user = GetUserByName(userName);
                if (null != user)
                {
                    if (!user.Channels.Contains(channel))
                    {
                        user.Channels.Add(channel);
                        RateUp(channel);
                        dataContext.SubmitChanges();
                    }
                }
            }
        }

        public void SignOut(string userName, Channels channel)
        {
            if ((null != channel) && (null != userName))
            {
                aspnet_Users user = GetUserByName(userName);
                if (null != user)
                {
                    if (user.Channels.Contains(channel))
                    {
                        user.Channels.Remove(channel);
                        RateDown(channel);
                        dataContext.SubmitChanges();
                    }
                }
            }
        }

        private void RateUp(Channels channel)
        {
            if(null != channel)
            {
                channel.Rate++;
                dataContext.SubmitChanges();
            }
        }

        private void RateDown(Channels channel)
        {
            if(null != channel)
            {
                if (channel.Rate > 0)
                {
                    channel.Rate--;
                    dataContext.SubmitChanges();
                }
            }
        }


        public void SetFavouriteChannel(string userName, int channelID)
        {
            if(null != userName)
            {
                ResetFavouriteChannel(userName);
                Users_Channels userChannel = dataContext.Users_Channels.FirstOrDefault(item => item.Channels.ID == channelID);
                if (null != userChannel)
                {
                    userChannel.IsFavourite = true;
                    dataContext.SubmitChanges();
                }
            }
        }

        public void ResetFavouriteChannel(string userName)
        {
            Channels oldFavouriteChannel = GetFavouriteChannel(userName);
            if (null != oldFavouriteChannel)
            {
                Users_Channels userChannel = dataContext.Users_Channels.FirstOrDefault(item => item.Channels.ID == oldFavouriteChannel.ID);
                if(null != userChannel)
                {
                    userChannel.IsFavourite = false;
                    dataContext.SubmitChanges();
                }
            }
        }

        public bool IsVisibleByUser(Channels channel, string userName)
        {
            var user = GetUserByName(userName);
            if(null != user)
            {
                return (user.Channels.Contains(channel) || channel.Visible);
            }
            else
            {
                return false;
            }
        }
    }
}